import sys

from PyQt5.QtWidgets import QMainWindow, QApplication
from action.LoginAction import Action
from action.setupAction import SetUpAction
from requestDataBase.request import synData
from windows.loginWindow import Ui_LoginWindow
from windows.setUpWindow import Ui_SetUpWindow


class Main(QMainWindow):
    def __init__(self):
        super().__init__()
        self.sa = None
        self.ac = Action()
        self.MainWin = Ui_LoginWindow()
        self.MainWin.setupUi(self)
        self.setFixedSize(380, 352)
        self.loginDef()

    def loginDef(self):
        """登录界面关联方法"""
        self.MainWin.checkBox_autoLogin.stateChanged.connect(self.ac.checkBoxLogin)
        self.MainWin.checkBox_show.stateChanged.connect(self.ac.checkBoxSave)
        # self.MainWin.pushButton_setup.clicked.connect(self.ac.pushButtonSetup)
        self.MainWin.pushButton_setup.clicked.connect(self.setup_Win)
        self.MainWin.pushButton_Login.clicked.connect(self.ac.pushButtonLogin)

    def setup_Win(self):
        """打开配置界面"""
        self.MainWin = Ui_SetUpWindow()
        self.MainWin.setupUi(self)
        self.sa = SetUpAction(self.MainWin.statusbar,self.MainWin.comboBox_obj)
        self.setFixedSize(380, 159)
        self.MainWin.pushButton_add.clicked.connect(lambda: self.sa.add(objName=self.MainWin.lineEdit_objName.text(),
                                                                        url=self.MainWin.lineEdit_objUrl.text()))
        self.MainWin.pushButton_delete.clicked.connect(lambda: self.sa.delete(obj=self.MainWin.lineEdit_objName.text()))
        self.MainWin.pushButton_change.clicked.connect(lambda: self.sa.change(obj=self.MainWin.lineEdit_objName.text(),
                                                                              url=self.MainWin.lineEdit_objUrl.text()))
        self.MainWin.pushButton_back.clicked.connect(self.login_win)
        self.MainWin.pushButton_synchData.clicked.connect(lambda:synData())

    def login_win(self):
        self.MainWin = Ui_LoginWindow()
        self.MainWin.setupUi(self)
        self.setFixedSize(380, 352)
        self.loginDef()


if __name__ == '__main__':
    app = QApplication(sys.argv)

    myWin = Main()
    myWin.show()

    sys.exit(app.exec_())
